/*
 * This is a collection of functions and objects for managing the items and categories view 
 * It use a Object Model we will call the the catalog object model. It is like this:
 * class (object)
 *
 * orderCatalog (myCatalog)
 * orderCatalog::orderCategoriesList (myCategoriesList)
 * orderCatalog::orderCategoriesList::array(orderCategory) (categories)
 * orderCatalog::orderCategoriesList::orderCategory[x]::orderItemsList (itemsList)
 * orderCatalog::orderCategoriesList::orderCategory[x]::orderItemsList:array(smallItem) (items)
 */
 
 // myCatalog is the global variable that contain the catalog object model objects. Any time we need to know the catalog
 // state or to modify any data from it we will do it through this variable. We initialize this variable here.


// This function sets the categories and subcategories from a object contained that is defined at the string by the standard JSON
// Categories and subcategories are defined normally by steps. That's mean that first we load root categories and then
// subcategories are being loaded at the correspondents categories

function adminCatalogNode(){
	catalogNode.call(this);
	this.myCategory=new adminCategory();
	this.myCategory.parentNode=this;
}

adminCatalogNode.prototype=Object.create(catalogNode.prototype);

adminCatalogNode.prototype.constructor=catalogNode;

adminCatalogNode.prototype.loadCategories=function(caregoriesString) {
	var categoriesPointer=JSON.parse(caregoriesString);
	this.children=new Array();
	// These are root categories
	for (var i in categoriesPointer.categories) {
		this.children[i]=new adminCatalogNode();
		this.children[i].parentNode=this;
		this.children[i].myCategory.setFromArray(categoriesPointer.categories[i]);
	}
}

// Equivalent to refreshOrderView but for the categoriesList
adminCatalogNode.prototype.refreshCategoriesListView=function (elementId, root) {
	catalogNode.prototype.refreshCategoriesListView.call(this,elementId);
	myCategoriesListElements=this.myCategoriesView.getElementsByTagName("*");
	for (var i in myCategoriesListElements) {
		if (typeof myCategoriesListElements[i].getAttribute != 'function') continue;
		if (myCategoriesListElements[i].getAttribute("myProperty")=="addCategory") {
			if (this.myCategory.id) myCategoriesListElements[i].href+=this.myCategory.id;
			else myCategoriesListElements[i].href+="0";
			initLoadingElements(myCategoriesListElements[i]);
			break;
		}
	}
};

adminCatalogNode.prototype.refreshItemsListView=function(elementId) {
	catalogNode.prototype.refreshItemsListView.call(this,elementId);
	myItemsListElements=this.myItemsView.getElementsByTagName("*");
	for (var i in myItemsListElements) {
		if (typeof myItemsListElements[i].getAttribute != 'function') continue;
		if (myItemsListElements[i].getAttribute("myProperty")=="addItem") {
			if (this.myCategory) myItemsListElements[i].href+=this.myCategory.id;
			else myCategoriesListElements[i].href+="0";
			initLoadingElements(myItemsListElements[i]);
			break;
		}
	}
};

// This function load the items in a category from a json formated string with the items
adminCatalogNode.prototype.loadItems=function (itemsString) {
	var itemPointer=JSON.parse(itemsString);
	this.items=new Array();
	// Now we set the items at the selected category
	for (var i in itemPointer.items) {
		this.items[i]=new adminItem();
		this.items[i].setFromArray(itemPointer.items[i]);
		this.items[i].parentNode=this;
	}

};

function adminCategory() {
	category.call(this);
}

adminCategory.prototype=Object.create(category.prototype);

adminCategory.prototype.constructor=adminCategory;

adminCategory.prototype.mySetView = function (myCategoryView) {
	category.prototype.mySetView.call(this,myCategoryView);
		var myCategoryElements=this.myView.getElementsByTagName("*");
		// It will search for the elements that correspond with the properties names to fill them
		for (var j in myCategoryElements) {
			if (typeof myCategoryElements[j].getAttribute != 'function') continue;
			// It defines the edit category button performance
			if (myCategoryElements[j].getAttribute("myProperty") == "edit") {
				var myForm=myCategoryElements[j].getElementsByTagName("form");
				var myFormClone=myForm[0].cloneNode(true);
				myCategoryElements[j].removeChild(myForm[0]);
				myCategoryElements[j].editForm=myFormClone;
				myCategoryElements[j].thisCategory=this;
				myFormClone.action+=this.id;
				myCategoryElements[j].onclick=function () {
					var myPointer=this.parentNode;
					while (myPointer.children[0]) myPointer.removeChild(myPointer.children[0]);
					this.editForm[this.getAttribute("myFor")].value = this.thisCategory[this.getAttribute("myFor")];
					initLoadingElements(this.editForm);
					myPointer.appendChild(this.editForm);
					this.editForm[this.getAttribute("myFor")].focus();
					return false;
				}
				break;
			}
		}
		var myCategoryElements=this.myView.getElementsByTagName("*");
		// It will search for the elements that correspond with the properties names to fill them
		for (var j in myCategoryElements) {
			// It defines the del category button performance
			if (typeof myCategoryElements[j].getAttribute != 'function') continue;
			if (myCategoryElements[j].getAttribute("myProperty") == "del") {
				myCategoryElements[j].href+=this.id;
				initLoadingElements(myCategoryElements[j]);
				break;
			}
		}
}


function adminItem() {
	catalogItem.call(this);
}

adminItem.prototype=Object.create(catalogItem.prototype);

adminItem.prototype.constructor=adminItem;

adminItem.prototype.mySetView=function(myItemView)  {
		catalogItem.prototype.mySetView.call(this,myItemView);
		var hasEdit=true;
		while(hasEdit) {
			myItemElements=this.myView.getElementsByTagName("*");
			for (var j in myItemElements) {
				hasEdit=false;
				if (typeof myItemElements[j].getAttribute != 'function') continue;
				// It defines the edit category button performance
				if (myItemElements[j].getAttribute("myProperty") == "edit" && !myItemElements[j].editForm) {
					var myForm=myItemElements[j].getElementsByTagName("form");
					var myFormClone=myForm[0].cloneNode(true);
					myItemElements[j].removeChild(myForm[0]);
					myItemElements[j].editForm=myFormClone;
					myItemElements[j].thisItem=this;
					myFormClone.action+=this.id;
					myItemElements[j].onclick=function () {
						var myPointer=this.parentNode;
						while (myPointer.children[0]) myPointer.removeChild(myPointer.children[0]);
						this.editForm[this.getAttribute("myFor")].value = this.thisItem[this.getAttribute("myFor")];
						initLoadingElements(this.editForm);
						myPointer.appendChild(this.editForm);
						this.editForm[this.getAttribute("myFor")].focus();
						return false;
					}
					hasEdit=true;
					break;
				}
			}
		}
		var myItemElements=this.myView.getElementsByTagName("*");
		// It will search for the elements that correspond with the properties names to fill them
		for (var j in myItemElements) {
			// It defines the del category button performance
			if (typeof myItemElements[j].getAttribute != 'function') continue;
			if (myItemElements[j].getAttribute("myProperty") == "del" || myItemElements[j].getAttribute("myProperty") == "editImg") {
				myItemElements[j].href+=this.id;
				initLoadingElements(myItemElements[j]);
			}
		}
}